package com.ai.service.made.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ai.common.CommonResponse;
import com.ai.common.constant.Constants;
import com.ai.entity.alarm.DefectParam;
import com.ai.mapper.alarm.DefectParamMapper;
import com.ai.service.made.DefectParamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * @Author：xiaosheng
 * @Date：2024/5/31 15:26
 */
@Service
public class DefectParamServiceImpl implements DefectParamService {

    @Resource
    DefectParamMapper defectParamMapper;


    @Override
    public CommonResponse insert(DefectParam defectParam) {
        return CommonResponse.ok(defectParamMapper.insert(defectParam));
    }


    @Override
    public List<DefectParam> selectByAlarmAdditionId(Integer id) {
        if (id == null) {
            return Collections.emptyList();
        }
        QueryWrapper<DefectParam> queryWrapper = new QueryWrapper();
        queryWrapper.eq("alarm_addition_id", id);
        List<DefectParam> defectParams = defectParamMapper.selectList(queryWrapper);
        if (defectParams != null && !defectParams.isEmpty()) {
            return defectParams;
        }
        return Collections.emptyList();
    }

    @Override
    public HashMap<String, List<Integer>> getDefectIndexMap(Integer alarmAdditionId) {

        HashMap<String, List<Integer>> faceIndexMap = new HashMap<>(16);

        List<String> aFaceDefectIndexList = defectParamMapper.getFaceDefectIndexList(Constants.CGA_FACE_TYPE,alarmAdditionId);
        List<Integer> aFaceIndexResList = new ArrayList<>(Collections.nCopies(24, 0));
        if (CollectionUtil.isNotEmpty(aFaceDefectIndexList)){
            for (String index : aFaceDefectIndexList) {
                aFaceIndexResList.set(Integer.parseInt(index),1);
            }
        }

        List<String> bFaceDefectIndexList = defectParamMapper.getFaceDefectIndexList(Constants.CGB_FACE_TYPE,alarmAdditionId);
        List<Integer> bFaceIndexResList = new ArrayList<>(Collections.nCopies(24, 0));
        if (CollectionUtil.isNotEmpty(bFaceDefectIndexList)){
            for (String index : bFaceDefectIndexList) {
                bFaceIndexResList.set(Integer.parseInt(index),1);
            }
        }

        faceIndexMap.put("faceA",aFaceIndexResList);
        faceIndexMap.put("faceB",bFaceIndexResList);

        return faceIndexMap;
    }

    public HashMap<String,List<String>> getDefectTypeMap(Integer alarmAdditionId ){
        HashMap<String, List<String>> faceDefectTypeMap = new HashMap<>(16);

        //1.边损   2.边裂   3.毛刺    4.扎破
        List<String> aDefectTypeList = defectParamMapper.getDefectTypeMap(Constants.CGA_FACE_TYPE,alarmAdditionId);
        constructResList(Constants.CGA_FACE_TYPE,faceDefectTypeMap, aDefectTypeList);
        List<String> bDefectTypeList = defectParamMapper.getDefectTypeMap(Constants.CGB_FACE_TYPE, alarmAdditionId);
        constructResList(Constants.CGB_FACE_TYPE,faceDefectTypeMap,bDefectTypeList);

        return faceDefectTypeMap;
    }

    private static void constructResList(String faceType,HashMap<String, List<String>> faceDefectTypeMap, List<String> defectTypeList) {
        List<String> isDefectList = new ArrayList<>(Collections.nCopies(5, Constants.UN_DEFECTION));

        if (!CollectionUtils.isEmpty(defectTypeList)){
            for (String index : defectTypeList) {
                isDefectList.set(Integer.parseInt(index),Constants.IS_DEFECTION);
            }
        }else {
            isDefectList.set(0,Constants.IS_DEFECTION);
        }
        String face = faceType.equals(Constants.CGA_FACE_TYPE)?"faceA":"faceB";
        faceDefectTypeMap.put(face,isDefectList);
    }


}
